package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.integral;

import android.app.Activity;
import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.MemberInfoBean;
import com.zzsy.carosshopprojects.bean.MemberInfoResultBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import androidx.lifecycle.MutableLiveData;

public class IntegralMode extends BaseViewMode {
    private MutableLiveData<MemberInfoResultBean> liveResult;
    private MutableLiveData<Integer> liveCode;

    public MutableLiveData<MemberInfoResultBean> getLiveData() {
        if (liveResult == null)
            liveResult = new MutableLiveData<>();
        return liveResult;
    }

    public MutableLiveData<Integer> getLiveCodeData() {
        if (liveCode == null)
            liveCode = new MutableLiveData<>();
        return liveCode;
    }

    /**
     * 根据UUID查询会员信息
     *
     * @param context
     * @param uuid
     * @param
     */
    public void MemberByUuid(Activity context, String uuid) {
        RequestUtils.MemberByUuid(context, uuid, DataUtil.getInstance().getCompanyId(), new MyObserver<MemberInfoBean>(context, false) {
            @Override
            public void onSuccess(MemberInfoBean bean) {
                String phone = bean.getPhone();
                String memberId = bean.getMemberId();
                String realName = bean.getRealName();
                int integral = bean.getIntegral();
                int newIntegral =ConverterHelper.toBigDecimal(integral).intValue();
                if (StringUtils.isEmpty(memberId)) {
                    ToastUtils.showToast(context.getApplicationContext(), "该会员异常！");
                    context.finish();
                }
                if (StringUtils.isEmpty(phone)) {
                    phone = "暂无";
                } else {
                    phone = "电话号:" + phone;
                }
                if (StringUtils.isEmpty(realName)) {
                    realName = "暂无";
                }
                MemberInfoResultBean memberInfoResultBean = new MemberInfoResultBean();
                memberInfoResultBean.setPhone(phone);
                memberInfoResultBean.setPhones(bean.getPhone());
                memberInfoResultBean.setRealName(realName);
                memberInfoResultBean.setNewIntegral(newIntegral);
                liveResult.postValue(memberInfoResultBean);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });
    }

    /**
     * 积分的扣除
     *
     * @param context
     * @param phone
     * @param integral
     */
    public void IntegralPay(Activity context, String phone, String integral) {
        RequestUtils.IntegralPay(context, phone, integral, new MyObserver<BaseBean>(context) {
            @Override
            public void onSuccess(BaseBean result) {
                liveCode.postValue(200);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
                    liveCode.postValue(200);
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), errorMsg);
                    context.finish();
                }
            }
        });
    }
}
